package com.hubing.cloud.app.web.service.impl;

import com.hubing.cloud.app.web.mapper.SearchHotMapper;
import com.hubing.cloud.app.web.mapper.SearchMapper;
import com.hubing.cloud.app.web.model.entity.Search;
import com.hubing.cloud.app.web.model.entity.SearchHot;
import com.hubing.cloud.app.web.service.SearchHotService;
import com.hubing.cloud.app.web.util.ApplicationContextHelper;
import com.hubing.cloud.app.web.util.DateUtil;
import com.hubing.cloud.app.web.util.IdUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
* @author 81517
* @description 针对表【search_hot】的数据库操作Service实现
* @createDate 2023-01-31 01:42:54
*/
@Service
public class SearchHotServiceImpl implements SearchHotService {

    @Autowired
    private SearchHotMapper searchHotMapper;

    @Autowired
    private SearchMapper searchMapper;
    /**
     * 查询热搜
     *
     * @return
     */
    @Override
    public List<String> queryHot() {
        List<SearchHot> hotList = searchHotMapper.selectAll();
        List<String> list = null;
        // 生成热搜
        if (CollectionUtils.isEmpty(hotList)) {
            list = ApplicationContextHelper.getBean(SearchHotServiceImpl.class).makeHot();
        } else {
            list = hotList.stream().map(SearchHot::getSearchHotContent).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * 生成热搜
     *
     * @return
     */
    @Transactional
    @Override
    public List<String> makeHot() {
        // 1天内、1周内、1月内、3月内、所有
        Date now = new Date();
        Date start = null;

        Date yesterday = DateUtil.getDate(Calendar.DAY_OF_WEEK, -1);
        Date week = DateUtil.getDate(Calendar.DAY_OF_WEEK, -7);
        Date month = DateUtil.getDate(Calendar.MONTH, -1);
        Date threemonth = DateUtil.getDate(Calendar.MONTH, -3);

        // 1天内
        Example example = new Example(Search.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andBetween("created",yesterday,now);
        int count = searchMapper.selectCountByExample(example);
        start = count >= 100 ? yesterday : null;

        // 1周内
        if (null == start) {
            Example example2 = new Example(Search.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andBetween("created",week,now);
            count = searchMapper.selectCountByExample(example2);
            start = count >= 100 ? week : null;
        }

        // 1月内
        if (null == start) {
            Example example3 = new Example(Search.class);
            Example.Criteria criteria3 = example3.createCriteria();
            criteria3.andBetween("created",month,now);
            count = searchMapper.selectCountByExample(example3);
            start = count >= 100 ? month : null;
        }

        // 3月内
        if (null == start) {
            Example example4 = new Example(Search.class);
            Example.Criteria criteria4 = example4.createCriteria();
            criteria4.andBetween("created",threemonth,now);
            count = searchMapper.selectCountByExample(example4);
            start = count >= 100 ? threemonth : null;
        }

        List<String> hotContentList = searchMapper.makeHot(start,now);


        // 重置热搜
        Example example5 = new Example(SearchHot.class);
        searchHotMapper.deleteByExample(example5);

        AtomicInteger sort = new AtomicInteger(1);
        hotContentList.forEach(item -> {
            SearchHot hot = new SearchHot();
            hot.setSearchHotId(IdUtil.getIncreaseIdByCurrentTimeMillis());
            hot.setSearchHotContent(item);
            hot.setCreated(now);
            hot.setVersion(1L);
            hot.setSort(sort.getAndIncrement());
            searchHotMapper.insert(hot);
        });

        return hotContentList;
    }
}
